package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OfferRelInfoModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OfferRelModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferDetailCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 融合销售品明细
 * @author zou.qinghua
 *
 */
@Service
public class OfferRelModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        List<KiteOfferInst>  listGiftOfferInsts = querySceneInstService.listGiftOfferInsts(sceneInstId);
        List<OfferRelInfoModuleVO> rels = new ArrayList<>();
        OfferRelModuleVO  offerRelModuleVO = new OfferRelModuleVO();
        if (KiteListUtils.isEmpty(listGiftOfferInsts)) {
            return rels;
        }
        KiteOfferInst giftOfferInst = listGiftOfferInsts.get(0);
        offerRelModuleVO.setOfferInstId(giftOfferInst.getOfferInstId());
        List<KiteAttr> attrs = giftOfferInst.listAttrs();
        if(KiteListUtils.isNotEmpty(attrs)){
            attrs.removeIf(attr -> KeyConsts.PROD_ATTR_SHOW_TYPE_H.equals(attr.getShowType()) || KeyConsts.PROD_ATTR_SHOW_TYPE_E.equals(attr.getShowType()));
        }
        for(KiteAttr attr : attrs){
            attr.setIsEdit("T");
        }
        offerRelModuleVO.setOfferAttrs(attrs);
        List<OfferDetail> offerDetails = OfferDetailCache.get(giftOfferInst.getOfferId());
        List<ProductModuleVO> products = sceneNewOfferSeneVO.getSceneProductModuleVO().getProduct();
        for(OfferDetail offerDetail : offerDetails) {
            // 礼包的产品明细不处理
            if (CoreDataOffer.OFF_C_0044_100000.equals(offerDetail.getObjType())) {
                continue;
            }
            String objId = offerDetail.getObjId();
            Offer detailOffer = OfferCache.get(objId);
            OfferRelInfoModuleVO offerRelInfoModuleVO = new OfferRelInfoModuleVO();
            List<OfferDetail> childOfferDetails = OfferDetailCache.get(objId);
            if(KiteListUtils.isNotEmpty(childOfferDetails) && KiteListUtils.isNotEmpty(products)){
                for(OfferDetail childOfferDetail : childOfferDetails){
                    for(ProductModuleVO productModuleVO : products){
                        if(KiteStringUtils.equals(childOfferDetail.getObjId(),productModuleVO.getProdId())){
                            productModuleVO.setOfferId(childOfferDetail.getOfferId());
                        }
                    }
                }
            }
            offerRelInfoModuleVO.setOfferName(detailOffer.getOfferName());
            offerRelInfoModuleVO.setOfferId(objId);
            offerRelInfoModuleVO.setOfferType(detailOffer.getOfferType());
            //            AttrValue attrValue = AttrValueCache.getAttrValue("230000401",detailOffer.getOfferType());
            //            offerRelInfoModuleVO.setOfferTypeName(attrValue.getAttrValueName());
            if (KiteListUtils.isNotEmpty((List) OfferDetailCache.get(objId))) {
                List<OfferDetail> offerDetailList = (List) OfferDetailCache.get(objId);
                offerRelInfoModuleVO.setOfferTypeName(offerDetailList.get(0).getRoleName());
            }
            offerRelInfoModuleVO.setMaxNum(offerDetail.getMaxNum());
            offerRelInfoModuleVO.setMinNum(offerDetail.getMinNum());
            List<KiteOfferInst> offerDetailInsts = querySceneInstService.listMainOfferInsts(sceneInstId,objId);
            offerRelInfoModuleVO.setCurrentNum(String.valueOf(offerDetailInsts.size()));
            rels.add(offerRelInfoModuleVO);
        }
        // 追加配件不展示成员商品列表
        if (!KiteStringUtils.isEqual("2020", context.getAcceptRequest().getServiceOfferId())) {
            offerRelModuleVO.setOfferRelInfoModuleVO(rels);
        }

        offerRelModuleVO.setIsEdit("T");
        offerRelModuleVO.setIsShow("T");
        offerRelModuleVO.setLayoutPage(sceneModule.getKiteModule().getLayoutPage());
        offerRelModuleVO.setModuleName(giftOfferInst.getOfferName());
        offerRelModuleVO.setOrderId(sceneModule.getKiteModule().getOrderId());
        sceneNewOfferSeneVO.getSceneProductModuleVO().setOfferRelModuleVO(offerRelModuleVO);
        return rels;
    }


    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        List<KiteOfferInst>  listGiftOfferInsts = querySceneInstService.listGiftOfferInsts(sceneInstId);
        List<OfferRelInfoModuleVO> rels = new ArrayList<>();
        OfferRelModuleVO  offerRelModuleVO = new OfferRelModuleVO();
        if (KiteListUtils.isEmpty(listGiftOfferInsts)) {
            return rels;
        }
        KiteOfferInst giftOfferInst = listGiftOfferInsts.get(0);
        offerRelModuleVO.setOfferInstId(giftOfferInst.getOfferInstId());
        List<KiteAttr> attrs = giftOfferInst.listAttrs();
        if(KiteListUtils.isNotEmpty(attrs)){
            attrs.removeIf(attr -> KeyConsts.PROD_ATTR_SHOW_TYPE_H.equals(attr.getShowType()) || KeyConsts.PROD_ATTR_SHOW_TYPE_E.equals(attr.getShowType()));
        }
        for(KiteAttr attr : attrs){
            attr.setIsEdit("F");
        }
        offerRelModuleVO.setOfferAttrs(attrs);
        List<OfferDetail> offerDetails = OfferDetailCache.get(giftOfferInst.getOfferId());
        List<ProductModuleVO> products = sceneNewOfferSeneVO.getSceneProductModuleVO().getProduct();
        for(OfferDetail offerDetail : offerDetails){
            if (CoreDataOffer.OFF_C_0044_100000.equals(offerDetail.getObjType())) {
                continue;
            }
            String objId = offerDetail.getObjId();
            Offer detailOffer = OfferCache.get(objId);
            OfferRelInfoModuleVO offerRelInfoModuleVO = new OfferRelInfoModuleVO();
            List<OfferDetail> childOfferDetails = OfferDetailCache.get(objId);
            if(KiteListUtils.isNotEmpty(childOfferDetails) && KiteListUtils.isNotEmpty(products)){
                for(OfferDetail childOfferDetail : childOfferDetails){
                    for(ProductModuleVO productModuleVO : products){
                        if(KiteStringUtils.equals(childOfferDetail.getObjId(),productModuleVO.getProdId())){
                            productModuleVO.setOfferId(childOfferDetail.getOfferId());
                        }
                    }
                }
            }
            offerRelInfoModuleVO.setOfferName(detailOffer.getOfferName());
            offerRelInfoModuleVO.setOfferId(objId);
            offerRelInfoModuleVO.setOfferType(detailOffer.getOfferType());
//            AttrValue attrValue = AttrValueCache.getAttrValue("230000401",detailOffer.getOfferType());
//            offerRelInfoModuleVO.setOfferTypeName(attrValue.getAttrValueName());
            if (KiteListUtils.isNotEmpty((List) OfferDetailCache.get(objId))) {
                List<OfferDetail> offerDetailList = (List) OfferDetailCache.get(objId);
                offerRelInfoModuleVO.setOfferTypeName(offerDetailList.get(0).getRoleName());
            }
            offerRelInfoModuleVO.setMaxNum(offerDetail.getMaxNum());
            offerRelInfoModuleVO.setMinNum(offerDetail.getMinNum());
            List<KiteOfferInst> offerDetailInsts = querySceneInstService.listMainOfferInsts(sceneInstId,objId);
            offerRelInfoModuleVO.setCurrentNum(String.valueOf(offerDetailInsts.size()));
            rels.add(offerRelInfoModuleVO);
        }
        // 追加配件不展示成员商品列表
        if (!KiteStringUtils.isEqual("2020", context.getAcceptRequest().getServiceOfferId())) {
            offerRelModuleVO.setOfferRelInfoModuleVO(rels);
        }
        offerRelModuleVO.setIsEdit("F");
        offerRelModuleVO.setIsShow("T");
        offerRelModuleVO.setLayoutPage(sceneModule.getKiteModule().getLayoutPage());
        offerRelModuleVO.setModuleName(giftOfferInst.getOfferName());
        offerRelModuleVO.setOrderId(sceneModule.getKiteModule().getOrderId());
        sceneNewOfferSeneVO.getSceneProductModuleVO().setOfferRelModuleVO(offerRelModuleVO);
        return rels;
    }
}
